package cn.zsb.service;

import cn.zsb.dao.AdminDao;
import cn.zsb.entity.Admin;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;


import cn.zsb.entity.*;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import javax.annotation.PreDestroy;

@Service
//@Scope("singleton")
@Scope("prototype")
@Transactional(readOnly = true, timeout = -1, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class AdminService {
    public static final Logger Log = LoggerFactory.getLogger(AdminService.class);
    @Autowired
    private UserService userService;
    @Autowired
    private AdminDao dao;

    public AdminService(){
        System.out.println("AdminService 被调用了。。。");
    }
    @PreDestroy
    public void xiaohui(){
        System.out.println("AdminService被销毁了。。。");
    }
    public Page<Admin> page(int pageNo, int pageSize) {

        List<Admin> list = dao.search1(pageNo, pageSize);
        int total = dao.findTotal();
        return new Page<>(list, pageNo, total, pageSize);

    }

    public int findTotal() {

        return dao.findTotal();

    }

    @Transactional(readOnly = false)
    public int add(Admin a) {
        User user = new User();
        user.setSname(a.getSadmin());
        user.setPass(a.getSpass());
        user.setOpenid(a.getOpenid());
        user.setPhone(a.getPhone());
        int add = dao.add(a);
        this.userService.add(user);
        return add;
    }

    public Admin checklogin(String sadmin, String spass) {

        return dao.login(sadmin, spass);

    }
}
